package yf.mall.controller.api;

import com.github.pagehelper.PageHelper;
import com.yf.annotation.LoginUserId;
import yf.mall.entity.CourseCardEntity;
import yf.mall.entity.CourseEntity;
import yf.mall.entity.SignEntity;
import com.yf.response.R;
import com.yf.response.RRException;
import yf.mall.service.CourseService;
import yf.mall.service.SignService;
import yf.mall.vo.CourseSumVo;
import yf.mall.vo.SignVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/course")
public class ApiCourseController {

    @Autowired
    private CourseService courseService;

    @RequestMapping("/all")
    public R courseList(Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<CourseEntity> courseEntities = courseService.findByWait();
        return R.ok(courseEntities);
    }


    /**
     * 班列表
     *
     * @return
     */
    @RequestMapping("/list")
    public R courseList(Long goodsId) {
        List<CourseEntity> courseEntities = courseService.findByWait(goodsId);
        return R.ok(courseEntities);
    }

    /**
     * 班列表
     *
     * @return
     */
    @RequestMapping("/info")
    public R info(Long courseId) {
        CourseEntity courseEntity = courseService.get(courseId);
        return R.ok(courseEntity);
    }

    /**
     * 新增课程卡券
     * 并且直接支付
     *
     * @param userId
     * @param goodsId
     * @return
     */
    @PostMapping("/card/create")
    public R createCard(@LoginUserId Long userId, Long storeId) {
        courseService.initCourseCard(userId, storeId);
        return R.ok();
    }

    /**
     * 卡券类表
     *
     * @return
     */
    @RequestMapping("/card/list")
    public R cardList(@LoginUserId Long userId) {
        List<CourseCardEntity> courseCardEntities = courseService.findCardByUserId(userId);
        return R.ok(courseCardEntities);
    }

    /**
     * 赠送课程卡券
     *
     * @param userId
     * @param toUserId
     * @param cardId
     * @return
     */
    @RequestMapping("/card/send")
    public R sendCard(@LoginUserId Long userId, Long toUserId, Long cardId) {
        CourseCardEntity courseCardEntity = courseService.sendCard(userId, cardId, toUserId);
        return R.ok(courseCardEntity);
    }


    /**
     * 扫描卡券,获取卡券或者消费卡券
     *
     * @return
     */
    @RequestMapping("/card/scan")
    public R scanCard(@LoginUserId Long userId, String code) {
        CourseCardEntity courseCardEntity = courseService.scan(userId, code);
        return R.ok(courseCardEntity);
    }


    /**
     * 报名
     *
     * @return
     */
    @RequestMapping("/sign")
    public R sign(@LoginUserId Long userId, String userName, String phone, String remark, Long courseId, Long cardId) {
        SignEntity signEntity = courseService.sign(userName, phone, remark, courseId, cardId, userId);
        return R.ok(signEntity);
    }

    /**
     * 报名列表
     *
     * @return
     */
    @RequestMapping("/sign/list")
    public R sign(@LoginUserId Long userId) {
        List<SignVo> signEntities = signService.findByUserId(userId);
        //统计
        signEntities.forEach(signVo -> {
            List<CourseSumVo> courseSumVos = courseService.sumSign(signVo.getCourse().getId());
            signVo.setSum(courseSumVos);
        });
        return R.ok(signEntities);
    }

    /**
     * 报名详情
     *
     * @param signId
     * @return
     */
    @RequestMapping("/sign/info")
    public R signInfo(Long signId) {
        SignEntity signEntity = signService.get(signId);
        CourseEntity courseEntity = courseService.get(signEntity.getCourseId());
        List<CourseEntity> courseEntities = courseService.findByWait(courseEntity.getGoodsId());
        return R.ok(signEntity).putExtra("courses", courseEntities).putExtra("courseName", courseEntity.getName());
    }

    /**
     * 报名确认
     *
     * @return
     */
    @RequestMapping("/sign/confirm")
    public R signConfirm(@LoginUserId Long userId, Long signId) {
        SignEntity signEntity = courseService.confirm(userId, signId);
        return R.ok(signEntity);
    }


    /**
     * 报名删除
     *
     * @return
     */
    @RequestMapping("/sign/remove")
    public R signRemove(@LoginUserId Long userId, Long signId) {
        SignEntity signEntity = signService.get(signId);
        if (!signEntity.getUserId().equals(userId)) {
            throw new RRException("非法操作!");
        }
        signService.delete(signId);
        return R.ok();
    }

    @Autowired
    private SignService signService;


}
